package com.yxhl.zoume.core.tripsmgmt.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.tripsmgmt.presenter.zoumebusticket.TicketRefundProgressPresenter;
import com.yxhl.zoume.core.tripsmgmt.view.zoumebusticket.BusRefundTicketProgressView;
import com.yxhl.zoume.data.http.model.trips.BusRefundProgressBean;
import com.yxhl.zoume.di.component.trip.DaggerTripComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.PromptUtils;

import javax.inject.Inject;

import butterknife.BindView;

public class BusTicketRefundProgressActivity extends BaseActivityWithToolBar implements BusRefundTicketProgressView {

    private static final String ORDER = "ORDER";
    @BindView(R.id.iv_refund_1)
    ImageView mIvRefund1;
    @BindView(R.id.iv_refund_2)
    ImageView mIvRefund2;
    @BindView(R.id.iv_refund_3)
    ImageView mIvRefund3;

    @BindView(R.id.tv_refund_commit)
    TextView mTvRefundCommit;

    @BindView(R.id.tv_refund_accept)
    TextView mTvRefundAccept;
    @BindView(R.id.tv_refund_accept_content)
    TextView mTvRefundAcceptContent;

    @BindView(R.id.tv_refund_end)
    TextView mTvRefundEnd;

    @BindView(R.id.tv_bus_refund_result_message)
    TextView mTvBusRefundResultMessage;

    @BindView(R.id.rl_bus_refund_result)
    RelativeLayout mRlBusRefundResult;
    private String mOrder;

    @Inject
    TicketRefundProgressPresenter mTicketRefundProgressPresenter;

    public static Intent getCallingIntent(Activity activity, String order) {
        Intent intent = new Intent(activity, BusTicketRefundProgressActivity.class);
        intent.putExtra(ORDER, order);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarTitle("退款进度");
        parseIntent();
        mTicketRefundProgressPresenter.attachView(this);
        if (!TextUtils.isEmpty(mOrder)) {
            mTicketRefundProgressPresenter.queryTicketRefundProgress(mOrder);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mTicketRefundProgressPresenter.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTicketRefundProgressPresenter.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTicketRefundProgressPresenter.onDestroy();
    }


    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mOrder = intent.getStringExtra(ORDER);
        }
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_trips_bus_refund01;
    }

    @Override
    protected void initializeInjector() {
        DaggerTripComponent
                .builder()
                .activityModule(new ActivityModule(this))
                .appComponent(getAppComponent())
                .build()
                .inject(this);
    }


    @Override
    public void renderRefundOnSuccess(BusRefundProgressBean busRefundProgressBean) {

        switch (busRefundProgressBean.getCurrRefundPos()) {
            case "1":
                mIvRefund1.setSelected(true);
                mTvRefundCommit.setSelected(true);
                break;
            case "2":
                mIvRefund1.setSelected(true);
                mTvRefundCommit.setSelected(true);
                if (busRefundProgressBean.isSuccFlag()) {
                    mIvRefund2.setSelected(true);
                    mTvRefundAccept.setSelected(true);
                } else {
                    mIvRefund2.setPressed(true);
                    mTvRefundAccept.setPressed(true);
                    mTvRefundAcceptContent.setText(busRefundProgressBean.getDesc());
                }
                break;
            case "3":
                mIvRefund1.setSelected(true);
                mTvRefundCommit.setSelected(true);
                mIvRefund2.setSelected(true);
                mTvRefundAccept.setSelected(true);
                if (busRefundProgressBean.isSuccFlag()) {
                    mIvRefund3.setSelected(true);
                    mTvRefundEnd .setSelected(true);
                } else {
                    mIvRefund3.setPressed(true);
                    mTvRefundEnd.setPressed(true);
                }

                break;
        }

        if (!TextUtils.isEmpty(busRefundProgressBean.getDesc()) && busRefundProgressBean.isSuccFlag()) {
            mRlBusRefundResult.setVisibility(View.VISIBLE);
            mTvBusRefundResultMessage.setText(busRefundProgressBean.getDesc());
        }
        reDraw();
    }

    @Override
    public void renderRefundOnFailure(String resultMessage) {
        PromptUtils.showShort(this, resultMessage);
    }

    @Override
    public void renderRefundOnError(String message) {
        PromptUtils.showShort(this, "网络繁忙，请稍后再试");
    }


    public void reDraw(){
        mIvRefund1.requestLayout();
        mIvRefund2.requestLayout();
        mIvRefund3.requestLayout();
    }
}
